﻿using System.Net;
using System.Web.Mvc;
using YogaStudio.Data.Domain;
using YogaStudio.Service.BLL;

namespace YogaStudio.WebUI.Controllers
{
    public class SchedulesController : Controller
    {
        //private YogaStudioEntities db = new YogaStudioEntities();
       private IScheduleService scheduleService=new ScheduleService();
        private  SemesterService semesterService=new SemesterService();

        // GET: Schedules
        public ActionResult Index()
        {
           
            return View(scheduleService.GetAllSchedules());
        }

        // GET: Schedules/Details/5
        public ActionResult Details(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult((int) HttpStatusCode.BadRequest);
            }
            Schedule schedule = scheduleService.GetSchedule((int)id);
            if (schedule == null)
            {
                return HttpNotFound();
            }
            return View(schedule);
        }

        // GET: Schedules/Create
        public ActionResult Create()
        {

            ViewBag.SemesterId = new SelectList(semesterService.GetAllSemesters(), "Id", "SemesterName");
            return View();
        }

        // POST: Schedules/Create
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create([Bind(Include = "Id,SemesterId,Year,FromDate,ToDate")] Schedule schedule)
        {
            if (ModelState.IsValid)
            {
                scheduleService.SaveSchedule(schedule);
                return RedirectToAction("Index");
            }

            ViewBag.SemesterId = new SelectList(semesterService.GetAllSemesters(), "Id", "SemesterName", schedule.SemesterId);
            return View(schedule);
        }

        // GET: Schedules/Edit/5
        public ActionResult Edit(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult((int) HttpStatusCode.BadRequest);
            }
            Schedule schedule =scheduleService.GetSchedule((int)id);
            if (schedule == null)
            {
                return HttpNotFound();
            }
            ViewBag.SemesterId = new SelectList(semesterService.GetAllSemesters(), "Id", "SemesterName", schedule.SemesterId);
            return View(schedule);
        }

        // POST: Schedules/Edit/5
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit([Bind(Include = "Id,SemesterId,Year,FromDate,ToDate")] Schedule schedule)
        {
            if (ModelState.IsValid)
            {
               scheduleService.UpdateSchedule(schedule);
                return RedirectToAction("Index");
            }
            ViewBag.SemesterId = new SelectList(semesterService.GetAllSemesters(), "Id", "SemesterName", schedule.SemesterId);
            return View(schedule);
        }

        // GET: Schedules/Delete/5
        public ActionResult Delete(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult((int) HttpStatusCode.BadRequest);
            }
            Schedule schedule = scheduleService.GetSchedule((int)id);
            if (schedule == null)
            {
                return HttpNotFound();
            }
            return View(schedule);
        }

        // POST: Schedules/Delete/5
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            Schedule schedule = scheduleService.GetSchedule((int)id);
            scheduleService.DeleteSchedule(schedule);
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
               scheduleService.Dispose(true);
            }
            base.Dispose(disposing);
        }
    }
}
